package com.system.jfm;
import com.system.jfm.Components.LabelPasswordTextField;
import com.system.jfm.Components.LabelTextField;
import com.system.jfm.Components.Message;
import com.system.Daos.AdminDao;
import com.system.Models.Admin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class RegisterFrame extends JFrame {
    RegisterFrame(){
        initFrame();
        this.setVisible(true);
    }
    public void initFrame(){
        JFrame r=this;
        this.setSize(550,400);
        this.setTitle("StuHub | 注册");
        this.getContentPane().setBackground(Color.white);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        this.setResizable(false);

        Image iconImage = Toolkit.getDefaultToolkit().getImage("src/com/system/image/icon.png");
        this.setIconImage(iconImage);
        ImageIcon imageIcon=new ImageIcon("src/com/system/image/StuHub.png");
        JButton title_btn=new JButton(imageIcon);
        title_btn.setBounds(130,50,imageIcon.getIconWidth(),imageIcon.getIconHeight());
        title_btn.setBorderPainted(false);
        this.add(title_btn);

        JLabel title_label = new JLabel("注册 StuHub");
        title_label.setSize(200,30);
        title_label.setLocation((int)(this.getWidth()/2-title_label.getWidth()/2+title_btn.getWidth()-40),50);
        Font font = new Font( "黑体",Font.PLAIN, 20);
        title_label.setFont(font);
        this.add(title_label);

        LabelTextField input1 = new LabelTextField("用户名:   ",250,30,0.2);
        input1.setLocation((int)(this.getWidth()/2-input1.getWidth()/2),title_label.getY()+50);
        input1.setPlaceHolder("请输入用户名");
        input1.label.setHorizontalAlignment(SwingConstants.RIGHT);
        input1.setBackground(Color.white);
        this.add(input1);

        LabelPasswordTextField input2=new LabelPasswordTextField("密码:   ",250,30,0.2);
        input2.setLocation((int)(this.getWidth()/2-input2.getWidth()/2),input1.getY()+50);
        input2.label.setHorizontalAlignment(SwingConstants.RIGHT);
        input2.setBackground(Color.white);
        this.add(input2);

        LabelPasswordTextField input3 = new LabelPasswordTextField("确认密码:   ",78,30,200,30);
        input3.setLocation((int)(this.getWidth()/2-input3.getWidth()/2-12),input2.getY()+50);
        input3.setBackground(Color.white);
        input3.label.setHorizontalAlignment(SwingConstants.RIGHT);
        this.add(input3);

        JButton register_btn=new JButton("确定");
        register_btn.setSize(150,30);
        register_btn.setLocation((int) (this.getWidth()/2-register_btn.getWidth()/2),input3.getY()+60);
        this.add(register_btn);

        JButton back_btn=new JButton("已有账号？返回");
        back_btn.setBounds(-25,335,150,30);
        back_btn.setBorderPainted(false);
        back_btn.setBackground(Color.white);
        this.add(back_btn);

        back_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                r.setVisible(false);
                new LoginFrame();
            }
        });
        register_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(input1.getText().equals("请输入用户名")){
                    JOptionPane.showMessageDialog(null,"用户名不能为空","提示",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if(input2.getText().equals("请输入密码")){
                    JOptionPane.showMessageDialog(null,"密码不能为空","提示",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if(input3.getText().equals("请重新确认密码")){
                    JOptionPane.showMessageDialog(null,"确认密码不能为空","提示",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!(input2.getText().equals(input3.getText()))){
                    JOptionPane.showMessageDialog(null,"密码与确认密码不一致","提示",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }

                String name = input1.getText();
                String pwd = input2.getText();

                Admin admin = new Admin(name,pwd);
                try {
                    if(new AdminDao(admin).addAdmin()){
                        r.setVisible(false);
                        new LoginFrame();
                    }else{
                        new Message("注册失败：名称已被占用");
                    }
                } catch (SQLException ex) {
                    new Message("注册失败：名称已被占用");
                    throw new RuntimeException(ex);
                }

            }
        });
    }
}
